package com.ctgu.fcftsystem.controller;

import com.ctgu.fcftsystem.pojo.User;
import com.ctgu.fcftsystem.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/UserController")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param uid
     * @param upwd
     * @return
     */
    @RequestMapping("/login")
    public Map login(String uid,String upwd){

        Map<String,Object> map = new HashMap<>();
        User user = userService.login(uid,upwd);
        if(user!=null){
            //登录成功
            map.put("code",0);
            map.put("msg","登录成功");
            map.put("data",user);
        }else{
            //登录失败
            map.put("code",1);
            map.put("msg","用户名或密码有误");
        }
        return map;
    }

    @RequestMapping("/getUserId")
    public Map getUserId(String uid){

        Map<String,Object> map = new HashMap<>();
        User user = userService.getUserId(uid);
        if(user==null){
            //登录成功
            map.put("code",0);
            map.put("msg","请继续完善信息");
        }else{
            //登录失败
            map.put("code",1);
            map.put("msg","该账号已经存在");
        }
        return map;
    }

    /**
     * 获取user表中所有的用户信息
     * @return
     */
    @RequestMapping("/getUserList")
    public Map getUserList(){
        Map<String,Object> map = new HashMap<>();
        List<User> list = userService.getUserList();
        if(list!=null&&!list.isEmpty()){
            //查询到所有用户
            map.put("code",0);
            map.put("msg","所有用户列表");
            map.put("data",list);
        }else{
            //未查到任何用户
            map.put("code",1);
            map.put("msg","没用用户");
        }
        return map;
    }

    /**
     * 添加用户
     * @param uid
     * @param uname
     * @param upwd
     * @param urole
     * @param uage
     * @param usex
     * @param umoney
     * @param usalary
     * @param ucard
     * @param utag
     * @return
     */
    @RequestMapping("/addUser")
    public Map addUser(String uid, String uname, String upwd, String urole, Integer uage, Integer usex, Integer umoney, Integer usalary, String ucard, Integer utag){
        Map<String,Object> map = new HashMap<>();
        if(uid!=null&&uname!=null&&upwd!=null&&urole!=null&&uage!=null&&usex!=null&&umoney!=null&&usalary!=null&&ucard!=null&&utag!=null){
            User user = new User();
            user.setUserId(uid);
            user.setUserName(uname);
            user.setUserPassword(upwd);
            user.setUserRole(urole);
            user.setUserAge(uage);
            user.setUserSex(usex);
            user.setUserMoney(umoney);
            user.setUserSalary(usalary);
            user.setUserCard(ucard);
            user.setAdminTag(utag);

            int res = userService.addUser(user);
            if(res>0){
                //添加成功
                map.put("code",0);
                map.put("msg","添加成功");
                map.put("data",res);
            }else{
                //添加失败
                map.put("code",1);
                map.put("msg","添加失败");
            }
        }else{
            //信息不完整
            map.put("code",1);
            map.put("msg","请填写完整信息");
        }
        return map;
    }

    /**
     * 根据uid删除用户信息
     * @param uid
     * @return
     */
    @RequestMapping("/delUserById")
    public Map delUserById(String uid){
        Map<String,Object> map = new HashMap<>();
        int res = userService.delUserById(uid);
        if(res == 1){
            //删除成功
            map.put("code",0);
            map.put("msg","删除成功");
            map.put("data",res);
        }else{
            //删除失败
            map.put("code",1);
            map.put("msg","删除失败");
        }
        return map;
    }

    /**
     * 更新用户信息
     * @param uid
     * @param uname
     * @param upwd
     * @param urole
     * @param uage
     * @param usex
     * @param umoney
     * @param usalary
     * @param ucard
     * @param utag
     * @return
     */
    @RequestMapping("/updateUser")
    public Map updateUser(String uid, String uname, String upwd, String urole, Integer uage, Integer usex, Integer umoney, Integer usalary, String ucard, Integer utag){
        Map<String,Object> map = new HashMap<>();
        User user = new User();

        user.setUserId(uid!=null?uid:uid);
        user.setUserName(uname!=null?uname:uname);
        user.setUserPassword(upwd!=null?upwd:upwd);
        user.setUserRole(urole!=null?urole:urole);
        user.setUserAge(uage!=null?uage:-1);
        user.setUserSex(usex!=null?usex:-1);
        user.setUserMoney(umoney!=null?umoney:-1);
        user.setUserSalary(usalary!=null?usalary:-1);
        user.setUserCard(ucard!=null?ucard:ucard);
        user.setAdminTag(utag!=null?utag:-1);

        int res = userService.updateUser(user);
        if(res>0){
            //修改成功
            map.put("code",0);
            map.put("msg","修改成功");
            map.put("data",res);
        }else{
            //修改失败
            map.put("code",1);
            map.put("msg","修改失败");
        }
        return map;
    }

    @RequestMapping("/updateUserPwd")
    public Map updateUserPwd(String uid,String upwd){
        Map<String,Object> map = new HashMap<>();
        if(upwd!=null){
            int res = userService.updateUserPwd(uid,upwd);
            if(res == 1){
                //修改成功
                map.put("code",0);
                map.put("msg","密码修改成功");
                map.put("data",res);
            }else{
                //修改失败
                map.put("code",1);
                map.put("msg","密码修改失败");
            }
        }else{
            map.put("code",1);
            map.put("msg","新密码不能为空");
        }
        return map;
    }

    @RequestMapping("/searchUser")
    public Map searchUser(String uid, String uname, String upwd, String urole, Integer uage, Integer usex, Integer umoney, Integer usalary, String ucard, Integer utag){
        Map<String,Object> map = new HashMap<>();
        User user = new User();

        user.setUserId(uid!=null?uid:uid);
        user.setUserName(uname!=null?uname:uname);
        user.setUserPassword(upwd!=null?upwd:upwd);
        user.setUserRole(urole!=null?urole:urole);
        user.setUserAge(uage!=null?uage:-1);
        user.setUserSex(usex!=null?usex:-1);
        user.setUserMoney(umoney!=null?umoney:-1);
        user.setUserSalary(usalary!=null?usalary:-1);
        user.setUserCard(ucard!=null?ucard:ucard);
        user.setAdminTag(utag!=null?utag:-1);

        List<User> list = userService.searchUser(user);
        if(list!=null&&!list.isEmpty()){
            //查询用户如下
            map.put("code",0);
            map.put("msg","查到用户如下");
            map.put("data",list);
        }else{
            //未查到任何用户
            map.put("code",1);
            map.put("msg","没用符合条件的用户");
        }
        return map;
    }

    /*分页查询*/
    @RequestMapping("/findByPaging")
    public Object findByPaging(int pageCount,int pageSize){
        ModelMap map = new ModelMap();
        PageHelper.startPage(pageCount, pageSize);//设置数据库分页查询的范围
        try {
            List<User> userList = userService.findByPaging(pageCount, pageSize);
            //分页的信息
            PageInfo<User>pageInfo=new PageInfo<>(userList);
            map.put("msg","查询所有角色成功");
            map.put("data",pageInfo);
            map.put("code",0);
        }catch (Exception e){
            e.getStackTrace();
            map.put("msg","查询所有角色失败");
            map.put("code",1);
        }
        return map;
    }

}
